using QFramework.Example.Scripts.Utility;

namespace QFramework.Example.Scripts.Model
{
    public interface IGameModel : IModel
    {
        public BindableProperty<int> KillCount { get; }
        public BindableProperty<int> Gold { get; }
        public BindableProperty<int> Score { get; }
        public BindableProperty<int> BestScore { get; }
        public BindableProperty<int> Life { get; }
    }

    public class GameModel : AbstractModel, IGameModel
    {
        public BindableProperty<int> KillCount { get; } = new()
        {
            Value = 0
        };

        public BindableProperty<int> Gold { get; } = new()
        {
            Value = 0
        };

        public BindableProperty<int> Score { get; } = new()
        {
            Value = 0
        };

        public BindableProperty<int> BestScore { get; } = new()
        {
            Value = 0
        };

        public BindableProperty<int> Life { get; } = new()
        {
            Value = 3
        };

        protected override void OnInit()
        {
            var storage = this.GetUtility<IStorage>();

            BestScore.Value = storage.LoadInt(nameof(BestScore));
            BestScore.Register(v => storage.SaveInt(nameof(BestScore), v));

            Life.Value = storage.LoadInt(nameof(Life), 3);
            Life.Register(v => storage.SaveInt(nameof(Life), v));

            Gold.Value = storage.LoadInt(nameof(Gold));
            Gold.Register(v => storage.SaveInt(nameof(Gold), v));
        }
    }
}